package com.lizardmind.haofudoctor.ui.diagnosis.diagnose

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.ke
import com.linxiao.framework.util.toasts
import com.linxiao.framework.widget.Section.SectionDecoration
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.*
import com.lizardmind.haofudoctor.other.rongYun.HaofuMessage
import com.lizardmind.haofudoctor.other.rongYun.MessageTags
import com.lizardmind.haofudoctor.other.rongYun.USER_PREFIX
import io.reactivex.functions.Consumer
import io.rong.imkit.RongIM
import io.rong.imlib.RongIMClient
import io.rong.imlib.model.Conversation
import io.rong.imlib.model.Message
import kotlinx.android.synthetic.main.activity_send_diagnose.*
import kotlinx.android.synthetic.main.include_title_text.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.toast

/**
 *  问诊单，发送问诊单
 * Created by Extends on 2017/11/9 15:05
 */
class SendDiagnoseActivity:BaseActivity() {
    private val userId by lazy { intent.getStringExtra("user_id") }
    private val apt by lazy { SendDiagnoseAdapter(this, arrayListOf<BaseDocumentsBean>()) }
    override fun onCreatRootView(): View? {
        setContentView(R.layout.activity_send_diagnose)
        return null
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        //初始化title
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        titleText.text = "发送问诊单"

        sd_rv.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = apt
            addItemDecoration(SectionDecoration(1, apt,false,0.3f))
        }
        apt.onSend={ _, data ->
            observe {
                postApi<Request<String>>(
                        hashMapOf("action" to "send_diagnose_doctor",
                                "baseId" to data,
                                "token" to DoctorInfo.token,
                                "user_id" to userId),
                        Consumer {
                            it.dispose {
                                RongIM.getInstance().sendMessage(
                                        Message.obtain(USER_PREFIX+userId, Conversation.ConversationType.PRIVATE, HaofuMessage(jsonOf("id" to it,"type" to "0"))),
                                        MessageTags.HAOFU,
                                        null,
                                        object : io.rong.imlib.IRongCallback.ISendMessageCallback {
                                            override fun onAttached(p0: Message) {}
                                            override fun onSuccess(p0: Message) {
                                                toasts("发送成功")
                                                finish()
                                            }
                                            override fun onError(p0: Message?, p1: RongIMClient.ErrorCode) {toasts("发送失败")}
                                        }
                                )
                            }
                        }
                )
            }
        }

        asd_add_new.onClick {
            toast("不知道跳到哪个界面")
        }

        observe {
            postApi<Request<DiagnoseManageBean>>(hashMapOf(
                    "action" to "manage_diagnose",
                    "token" to DoctorInfo.token
            ), Consumer {
                it.dispose {
                    apt.addAllAndClear(it.list)
                    sd_price.text = getString(R.string.price,it.price)
                }
            })
        }
    }


}